package cn.yh.register.hosp.api;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.helper.HttpRequestHelper;
import cn.yh.register.common.result.Result;
import cn.yh.register.common.result.ResultCodeEnum;
import cn.yh.register.hosp.service.DepartmentService;
import cn.yh.register.hosp.service.HospitalService;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.hosp.service.ScheduleService;
import cn.yh.register.model.hosp.Department;
import cn.yh.register.model.hosp.Hospital;
import cn.yh.register.model.hosp.Schedule;
import cn.yh.register.vo.hosp.DepartmentQueryVo;
import cn.yh.register.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "数据上传接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService setService;
    @Autowired  //部门
    DepartmentService departmentService;
    @Autowired  //科室
    ScheduleService scheduleService;

    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result<?> saveHospital(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service
        hospitalService.saveHospital(objectMap);
        return Result.ok();
    }

    @ApiOperation("查询医院信息")
    @PostMapping("/hospital/show")
    public Result<Hospital> getHospital(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);

        String hoscode = (String) objectMap.get("hoscode");
        //调用service
        Hospital hospital = hospitalService.getHospital(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传科室")
    @PostMapping("/saveDepartment")
    public Result<?> saveDepartment(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service
        departmentService.saveDepartment(objectMap);
        return Result.ok();
    }

    @ApiOperation("分页查询科室列表")
    @PostMapping("/department/list")
    public Result<Page<Department>> findByPage(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        Integer page = StringUtils.isEmpty(objectMap.get("page")) ? 1 : Integer.parseInt((String) objectMap.get("page"));
        Integer limit = StringUtils.isEmpty(objectMap.get("limit")) ? 10 : Integer.parseInt((String) objectMap.get("limit"));
        //查询条件
        DepartmentQueryVo queryVo = new DepartmentQueryVo();
        String hoscode = (String) objectMap.get("hoscode");
        queryVo.setHoscode(hoscode);

        //调用service
        Page<Department> byPage = departmentService.findByPage(page, limit, queryVo);
        return Result.ok(byPage);

    }

    @ApiOperation("删除科室")
    @PostMapping("/department/remove")
    public Result<?> removeDept(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //取出医院编号&科室编号
        String hoscode = (String) objectMap.get("hoscode");

        String deptcode = (String) objectMap.get("deptcode");
        //调用service
        departmentService.removeDept(hoscode, deptcode);
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result<?> saveSchedule(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service
        scheduleService.saveSchedule(objectMap);
        return Result.ok();
    }

    @ApiOperation("分页查询排班列表")
    @PostMapping("/schedule/list")
    public Result<Page<Schedule>> findScheduleByPage(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);

        Integer page = StringUtils.isEmpty(objectMap.get("page")) ? 1 : Integer.parseInt((String) objectMap.get("page"));
        Integer limit = StringUtils.isEmpty(objectMap.get("limit")) ? 10 : Integer.parseInt((String) objectMap.get("limit"));
        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        String hoscode = (String) objectMap.get("hoscode");
        queryVo.setHoscode(hoscode);
        Page<Schedule> bypage = scheduleService.findBypage(page, limit, queryVo);
        return Result.ok(bypage);

    }

    @ApiOperation("删除排班")
    @PostMapping("/schedule/remove")
    public Result<?> removeSchedule(HttpServletRequest request) {
        //处理参数 & 校验签名
        Map<String, Object> objectMap = this.handleParam(request);
        //调用service
        String hoscode = (String) objectMap.get("hoscode");
        String hosScheduleId = (String) objectMap.get("hosScheduleId");
        //调用service
        scheduleService.removeSchedule(hoscode,hosScheduleId);
        return Result.ok();
    }


    /**
     * 处理参数 & 校验签名
     *
     * @param request 请求对象
     * @return Map<String, Object>
     */
    public Map<String, Object> handleParam(HttpServletRequest request) {
        // 接受请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();

        // 转为ObjectMap
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);
        //参数传递过程中"+"转为了" " 导致签名校验错误，我们再把他们转回去
        String logoData = (String) objectMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)) {
            logoData = logoData.replaceAll(" ", "+");
            objectMap.put("logoData", logoData);
        }
        //获取医院编号
        String hoscode = (String) objectMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new RegisterException(ResultCodeEnum.PARAM_ERROR);
        }
        //获取签名Key
        String singKey = setService.getSingKey(hoscode);

        //校验签名
        if (!HttpRequestHelper.isSignEquals(objectMap, singKey)) {
            throw new RegisterException(ResultCodeEnum.SIGN_ERROR);
        }
        return objectMap;
    }


}
